<?php
/**
* 文件名(index.con.php)
*
* 功能描述（主页控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/
/*
update
V10－jlusuoya－20130827: 创建
update
*/
require('../smarty-3.1.13/Smarty.class.php');
require_once(MODEL_DIR . "userbase.php");
class CtrlBase extends Smarty
{
	public $user = null;
	public $params = array();
	public $paramCheck = array();
	public $search = array();		//搜索关键字
	public $page = array('cur'=>1, 'size'=>0, 'cnt' => 0, 'pageCnt' => 0);
	public $perms = array();
	public $base_perm = array(0, 255, "index.php");		//默认权限

	function __construct()
	{
		parent::__construct();
		$this->debugging = DEBUG;
		$this->caching = false;
		$this->cache_lifetime = 120;
		$this->left_delimiter = "{#";
		$this->right_delimiter = "#}";
		$this->template_dir = SRV_BASE . "templates/";
		$this->compile_dir = SRV_BASE . "templates_c/";
		$this->config_dir = SRV_BASE . "configs/";
		$this->cache_dir = SRV_BASE . "cache/";
		if(DEBUG)
			$this->force_compile = true;
		else
			$this->compile_check = true;
		//global var
		$mainMenu = array(
			array("index.php", "首页"),
			array("index.php?action=food.index", "我要订购"),
			array("index.php?action=user.index", "我的梵地"),
			array("index.php?action=guide.index", "订购指南"),
			array("index.php?action=vipnews.index", "VIP会员"),
			array("index.php?action=about.index", "认识梵地"),
			//array("index.php?action=message.index", "温馨语录"),
		);
		$sndMenu = array(
			array(
				array("index.php", "欢迎来到梵地"),
			),
			array(
				array("index.php?action=food.index", "销量排名"),
				array("index.php?action=food.index&type2=1", "新品推荐"),
				array("index.php?action=food.index&cheap=1", "打折促销"),
				array("index.php?action=food.index&type=2", "慕斯蛋糕"),
				array("index.php?action=food.index&type=3", "水果奶油蛋糕"),
				array("index.php?action=food.index&type=4", "巧克力蛋糕"),
				array("index.php?action=food.index&type=5", "乳脂奶油蛋糕"),
				array("index.php?action=food.index&type=6", "乳酪蛋糕"),
				array("index.php?action=food.index&type=7", "常温蛋糕"),
				array("index.php?action=food.index&type=1", "蛋糕配件"),
			),
			array(
				array("index.php?action=user.index", "会员首页"),
				array("index.php?action=user.info", "会员信息"),
				array("index.php?action=order.showlist", "我的订单"),
				array("index.php?action=user.address", "收货地址"),
				array("index.php?action=user.modpwd", "找回/修改密码"),
				array("index.php?action=user.lvmsg", "留言板"),
				array("index.php?action=favorite.showlist", "我的收藏"),
			),
			array(
				array("index.php?action=guide.index", "包装展示"),
				array("index.php?action=guide.buy", "订购说明"),
				array("index.php?action=guide.article3", "积分说明"),
				array("index.php?action=guide.getgoods", "如何收货"),
				array("index.php?action=guide.pay", "付款方式"),
				array("index.php?action=guide.scope", "配送范围"),
			),
			array(
				array("index.php?action=vipnews.index", "免费品尝"),
				array("index.php?action=vipnews.group", "大宗购买"),
			),
			array(
				array("index.php?action=about.index", "梵地的蛋糕"),
				//array("index.php?action=about.article1", "Chef说蛋糕"),
				//array("index.php?action=about.article2", "精品背后的努力"),
				array("index.php?action=about.article3", "梵地的故事"),
				array("index.php?action=contact.index", "联系我们"),
				array("#", "官方微博"),
			),
			/*
			array(
				array("index.php?action=message.index", "爱情"),
				array("index.php?action=message.list1", "友情"),
				array("index.php?action=message.list2", "亲情"),
				array("index.php?action=message.list3", "本月经典"),
			)
			*/
		);
		$this->assign("mainMenu", $mainMenu);
		$this->assign("sndMenu", $sndMenu);
		global $web_root;
		$this->assign("web_root", $web_root);
		$this->LoadInfo();
	}

	function LoadInfo()
	{
		//user
		$this->user = new UserBase();
		if($this->user->logined)
		{
			$this->assign("username", $this->user->username);
			$this->assign("com_name", $this->user->com_name);
		}
		else
			$this->ClearAssign(array("username", "com_name"));
		if(array_key_exists('cart', $_SESSION))
		{
			if(array_key_exists('cart_version', $_SESSION))
				$version = $_SESSION['cart_version'];
			else
				$version = 1;
			if($version != CART_VERSION)
			{
				unset($_SESSION['cart']);	//版本不对，后期要升级
				$cnt = 0;
			}
			else
				$cnt = count($_SESSION['cart']);
		}
		else
			$cnt = 0;		
		$_SESSION['cart_version'] = CART_VERSION;
		$this->assign("cartCnt", $cnt);
	}

	function GetModel($name)
	{
		$name = strtolower($name);
		require_once(MODEL_DIR . "$name.mod.php");
		$model = "Model" . $name;
		return new $model();
	}

	function GetCtrl($name)
	{
		$name = strtolower($name);
		require_once(CTRL_DIR . "$name.con.php");
		$ctrl = "Ctrl" . $name;
		return new $ctrl();
	}

	function CheckPermissions($method)
	{
		if(array_key_exists($method, $this->perms))
			$p = $this->perms[$method];
		else
			$p = $this->base_perm;
		$level = $this->user->level;
		if($level >= $p[0] && $level <= $p[1])	//权限范围内
			return;
		$_SESSION['before_login'] = $_SERVER['REQUEST_URI'];
		jump_to($p[2]);
	}

	function BindSession()
	{
		if(array_key_exists("g_sid", $_GET))
		{
			$cid = session_id();
			$nid = $_GET['g_sid'];
			if($cid != $nid)
				session_destroy();
			session_id($nid);
			setcookie('PHPSESSID', $nid);
			$_SESSION['bind'] = true;
		}
		$this->assign("bind", array_key_exists("bind", $_SESSION));
	}
}

